package com.fly.main;

import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.RandomUtils;
import org.springframework.context.support.GenericXmlApplicationContext;

import com.fly.demo.service.impl.CustomerServiceImpl;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * 测试Main
 * 
 * @author 00fly
 * @version [版本号, 2017-9-27]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class ThreadRun
{
    /**
     * Main
     * 
     * @param args
     * @throws InterruptedException
     * @see [类、类#方法、类#成员]
     */
    public static void main(String[] args)
        throws InterruptedException
    {
        // 线程运行中，context不能立即关闭
        try (GenericXmlApplicationContext context = new GenericXmlApplicationContext())
        {
            context.setValidating(false);
            context.load("classpath*:application*.xml");
            context.refresh();
            new MyThread().start();
            new MyThread().start();
            new MyThread().start();
            new MyThread().start();
            new MyThread().start();
            new MyThread().start();
            new MyThread().start();
            new MyThread().start();
            TimeUnit.SECONDS.sleep(20);
        }
        // context.close();
    }
    
}

/**
 * 
 * 单元测试线程
 * 
 * @author 00fly
 * @version [版本号, 2018年9月23日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Slf4j
class MyThread extends Thread
{
    public MyThread()
    {
        super();
    }
    
    @Override
    public void run()
    {
        try
        {
            // 此代码多次运行会报错 java.util.NoSuchElementException
            // 原因可能为BeanProcessor.toBeanList出现了越界.
            // CustomerServiceImpl.getInstance().queryAll();
            
            CustomerServiceImpl.getInstance().deleteById(1000);
            TimeUnit.SECONDS.sleep(RandomUtils.nextLong(1, 20));
            CustomerServiceImpl.getInstance().deleteById(1000);
        }
        catch (InterruptedException e)
        {
            log.error(e.getMessage(), e);
        }
    }
}
